<template>
  <div>
    
    <hr />
    当前专业
    <el-select
      v-model="form.subjectsId"
      placeholder="请选择"
      style="margin-top: 20px; margin-bottom: 5px"
    >
      <el-option
        v-for="item in subjects"
        :key="item.id"
        :label="item.name"
        :value="item.id"
      ></el-option>
    </el-select>
    <el-table
      :data="tableData"
      border
      style="width: 100%; margin-top: 20px; margin-bottom: 20px"
    >
      <el-table-column prop="_id" label="课程编号"> </el-table-column>
      <el-table-column prop="name" label="课程名称"> </el-table-column>
    

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit( scope.row)"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageData.currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageData.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      subjects: [],
      form: {
        name: "",
        subjectsId: "",
      },
      pageData: {
        currentPage: 1,
        pageSize: 5,
      },
      total: 0,
      searchData: {
        type: "",
      },
    };
  },
  created() {
    // this.getCourses();
     this.getSubjects();
    
  },
  //侦听页码变化
  watch: {
    pageData: {
      handler() {
        this.getCourses();
      },
      deep: true,
    },
  },
  methods: {
  //获取全部专业
    async getSubjects() {
      const res = await this.$api.subjects.get();
      console.log(res);

      if (res.code) {
        this.subjects = res.data.rows;
      }
    },
  
    // 删除一个课程数据
    async handleDelete(row) {
      console.log(row)
      const res = await this.$api.course.delete({ _id: row._id });
      console.log(res);
      if (res.code) {
        
        this.$message({
          message: "删除课程成功",
          type: "success",
        });
        this.getCourses();
      } 
    },
   
   

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageData.pageSize = val;
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageData.currentPage = val;
    },
  },
};
</script>

<style></style>
